package com.xinghen.ace.auth.interceptor;

import com.xinghen.ace.auth.configuration.ClientConfiguration;
import com.xinghen.ace.auth.service.AuthClientService;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author xinghen
 * @ClassName: ClientTokenInterceptor
 * @Package: com.xinghen.ace.auth.interceptor
 * @Description client token 拦截器
 * @create 2018/5/4 15:46
 */
@Slf4j
public class ClientTokenInterceptor implements RequestInterceptor {

    @Autowired
    private ClientConfiguration clientConfiguration;

    @Autowired
    private AuthClientService authClientService;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        try {
            requestTemplate.header(clientConfiguration.getClientTokenHeader(), authClientService.apply(clientConfiguration.getClientId(), clientConfiguration.getClientSecret()));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
